﻿using AuthCenter.Domain;
using AuthCenter.ErorCode;
using AuthCenter.Infrastructure;
using AuthCenter_API.Read.Applications.Commands.RBAC;
using MediatR;

namespace AuthCenter_API.Read.Applications.CommandHandlers.RBAC
{
    public class DelRoleDepCommandHandler : IRequestHandler<DelRoleDepCommand, APIResult<object>>
    {
        private readonly IBaseRepository<UserRoleModel> _urRepository;

        public DelRoleDepCommandHandler(IBaseRepository<UserRoleModel> urRepository)
        {
            _urRepository = urRepository;
        }

        public Task<APIResult<object>> Handle(DelRoleDepCommand request, CancellationToken cancellationToken)
        {
            APIResult<object> result = new APIResult<object>();
            result.Code=APIEnum.DelSuccess;
            result.Message=APIEnum.DelSuccess.Desc();
            var m = _urRepository.GetAll().Where(x => x.UserId == request.UserId && x.RoleId == request.RoleId).FirstOrDefault();
            if (m!=null)
            {
                _urRepository.Del(m);
            }
            return Task.FromResult(result);
        }
    }
}
